/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bff.gaia.runtime.clusterframework.overlays;

import com.bff.gaia.configuration.Configuration;
import com.bff.gaia.core.fs.Path;
import com.bff.gaia.runtime.clusterframework.ContainerSpecification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import static com.bff.gaia.configuration.ConfigConstants.ENV_GAIA_BIN_DIR;
import static com.bff.gaia.configuration.ConfigConstants.ENV_GAIA_CONF_DIR;
import static com.bff.gaia.configuration.ConfigConstants.ENV_GAIA_HOME_DIR;
import static com.bff.gaia.configuration.ConfigConstants.ENV_GAIA_LIB_DIR;
import static com.bff.gaia.util.Preconditions.checkNotNull;

/**
 * Overlays Gaia into a container, based on supplied bin/conf/lib directories.
 *
 * The overlayed Gaia is indistinguishable from (and interchangeable with)
 * a normal installation of Gaia.  For a docker image-based container, it should be
 * possible to bypass this overlay and rely on the normal installation method.
 *
 * The following files are copied to the container:
 *  - gaia/bin/
 *  - gaia/conf/
 *  - gaia/lib/
 */
public class GaiaDistributionOverlay extends AbstractContainerOverlay {

	private static final Logger LOG = LoggerFactory.getLogger(GaiaDistributionOverlay.class);

	static final Path TARGET_ROOT = new Path("gaia");

	final File gaiaBinPath;
	final File gaiaConfPath;
	final File gaiaLibPath;

	public GaiaDistributionOverlay(File gaiaBinPath, File gaiaConfPath, File gaiaLibPath) {
		this.gaiaBinPath = checkNotNull(gaiaBinPath);
		this.gaiaConfPath = checkNotNull(gaiaConfPath);
		this.gaiaLibPath = checkNotNull(gaiaLibPath);
	}

	@Override
	public void configure(ContainerSpecification container) throws IOException {

		container.getEnvironmentVariables().put(ENV_GAIA_HOME_DIR, TARGET_ROOT.toString());

		// add the paths to the container specification.
		addPathRecursively(gaiaBinPath, TARGET_ROOT, container);
		addPathRecursively(gaiaConfPath, TARGET_ROOT, container);
		addPathRecursively(gaiaLibPath, TARGET_ROOT, container);
	}

	public static Builder newBuilder() {
		return new Builder();
	}

	/**
	 * A builder for the {@link GaiaDistributionOverlay}.
	 */
	public static class Builder {
		File gaiaBinPath;
		File gaiaConfPath;
		File gaiaLibPath;

		/**
		 * Configures the overlay using the current environment.
		 *
		 * Locates Gaia using GAIA_???_DIR environment variables as provided to all Gaia processes by config.sh.
		 *
		 * @param globalConfiguration the current configuration.
		 */
		public Builder fromEnvironment(Configuration globalConfiguration) {

			Map<String,String> env = System.getenv();
			if(env.containsKey(ENV_GAIA_BIN_DIR)) {
				gaiaBinPath = new File(System.getenv(ENV_GAIA_BIN_DIR));
			}
			else {
				throw new IllegalStateException(String.format("the %s environment variable must be set", ENV_GAIA_BIN_DIR));
			}

			if(env.containsKey(ENV_GAIA_CONF_DIR)) {
				gaiaConfPath = new File(System.getenv(ENV_GAIA_CONF_DIR));
			}
			else {
				throw new IllegalStateException(String.format("the %s environment variable must be set", ENV_GAIA_CONF_DIR));
			}

			if(env.containsKey(ENV_GAIA_LIB_DIR)) {
				gaiaLibPath = new File(System.getenv(ENV_GAIA_LIB_DIR));
			}
			else {
				throw new IllegalStateException(String.format("the %s environment variable must be set", ENV_GAIA_LIB_DIR));
			}

			return this;
		}

		public GaiaDistributionOverlay build() {
			return new GaiaDistributionOverlay(gaiaBinPath, gaiaConfPath, gaiaLibPath);
		}
	}
}